//
//  YCNewsViewController.m
//  YCSchool
//
//  Created by yanglinxia on 16/4/28.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "YCNewsViewController.h"
#import "YCNewsCell.h"
#import "YCNewsModel.h"

@interface YCNewsViewController ()<UITableViewDelegate,UITableViewDataSource>

/** tableView */
@property (nonatomic, strong) UITableView *tableView;

/** 数据  */
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation YCNewsViewController

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        
        YCNewsModel *model = [[YCNewsModel alloc] init];
        model.title = @"公安部\"新驾考\"4月1日实施务必记住几个重点";
        model.icon = @"http://newsxml.cnool.net/newspic2011/2010/2010-12/2010-12-6/634272260653125000.jpg";
        model.time = 21243;
        model.comefromWebsite = @"凤凰网";
        
        YCNewsModel *model1 = [[YCNewsModel alloc] init];
        model1.title = @"公安部\"新驾考\"4月1日实施务必记住几个重点,大小家心啦";
        model1.icon = @"http://www.sxol.com/Editor1/uploadfile/20110514093406867.jpg";
        model1.time = 343;
        model1.comefromWebsite = @"中央台";
        
        YCNewsModel *model2 = [[YCNewsModel alloc] init];
        model2.title = @"公安部\"新驾考\"4月1日实施务必记住几个重点月1日实施务必记住几个重点";
        model2.icon = @"http://www.zgqczj.com/file/upload/201308/20/11-24-26-64-581.jpg";
        model2.time = 33;
        model2.comefromWebsite = @"汽车装具网";
        
        YCNewsModel *model3 = [[YCNewsModel alloc] init];
        model3.title = @"国家安全局表示:公安部\"新驾考\"4月1日实施务必记住几个重点";
        model3.icon = @"http://www.rtsac.org/Upload/UEditor/Img/20150507/6356659266748775001808843.jpg";
        model3.time = 5243;
        model3.comefromWebsite = @"一乘汽车网";
        
        YCNewsModel *model4 = [[YCNewsModel alloc] init];
        model4.title = @"国家安全局表示:公安部\"新驾考\"4月1日实施务必记住几个重点";
        model4.icon = @"http://www.rtsac.org/Upload/UEditor/Img/20150507/6356659266748775001808843.jpg";
        model4.time = 153;
        model4.comefromWebsite = @"一乘汽车网";
        
        [_dataArray addObjectsFromArray:@[model,model1,model2,model3,model4]];
        
    }
    return _dataArray;
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.yc_width, self.view.yc_height - 64) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor yc_colorWithUInt:0xf2f2f2];
        UINib *nib = [UINib nibWithNibName:NSStringFromClass([YCNewsCell class]) bundle:nil];
        [_tableView registerNib:nib forCellReuseIdentifier:@"newsCell"];
    }
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"资讯";
    [self yc_addBackBarButtonItem];
    [self.view addSubview:self.tableView];
    self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
    self.tableView.rowHeight = heightFromIphone6Height(144);// 132+间距12
    
    //下拉刷新
    YCWeakSelf;
    self.tableView.mj_header = [YCRefreshHeader headerWithRefreshingBlock:^{
        [weakSelf loadNewData];
    }];
    
    // 上拉刷新
    self.tableView.mj_footer = [YCRefreshFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreData];
    }];
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 数据处理相关
- (void)loadNewData
{
    // 1.添加数据
    
    // 模拟2秒后刷新表格UI
    YCWeakSelf;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [weakSelf.tableView reloadData];
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tableView.mj_header endRefreshing];
    });
}

// 上拉加载
- (void)loadMoreData
{
    // 添加数据
    BOOL flag = YES;//用来判断是不是还有数据
    
    // 模拟2秒后刷新表格UI
    YCWeakSelf
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [weakSelf.tableView reloadData];
        
        if (flag) {
            [weakSelf.tableView.mj_footer endRefreshing];//结束上拉刷新
        } else {
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];//没有更多数据可加载
        }
        //        weakSelf.tableView.mj_footer.hidden = YES;//只加载一次,只要hiden就可以了
    });
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newsCell"];
    cell.contentView.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
    cell.itemModel = self.dataArray[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCLogFunc
//    YCTrainCarDetailViewController *detailVc = [[YCTrainCarDetailViewController alloc] initWithModel:self.itemsArray[indexPath.row]];
//    [self.navigationController pushViewController:detailVc animated:YES];
    
}


#pragma mark - 点击事件



@end
